import { _decorator, Vec3 , Node, randomRangeInt} from 'cc';
import { SkillManager } from './SkillManager';
import { EGroup, EntityType, ESkill } from '../Config/Enum';
import ObjectPoolManager from '../Core/ObjectPoolManager';
import { Collider2D } from 'cc';
import { PlayerManager } from '../PlayerManager';
import { BattleManager } from '../BattleManager';
import { EnemyManager } from '../EnemyManager';

const { ccclass } = _decorator;
// 打狗棒法
//rune1 把周围的敌人吸过来
//rune2 每秒造成2次伤害
//rune3 掉宝率提升
@ccclass('GaibangSkill3')
export class GaibangSkill3 extends SkillManager {
    private _hasSplit = false;
    private _contactPosition: Vec3 = new Vec3(0, 0, 0);
    runeId = 0
    numOfBullets = 1; // 默认是3个
    level
    em
        // 添加变量存储DragEnemy节点
    private _dragEnemyNode: Node | null = null;

    // 是否是主子弹（由玩家直接发射的子弹）
    private isMasterBullet = true;


    init(type: ESkill, pm: PlayerManager) {

        // 如果是子子弹，直接super.init(type, pm); 直接释放
        if (!this.isMasterBullet) {
            super.init(type, pm);
            return;
        }

        if (this.isMasterBullet) {
            this._hasSplit = false;
        }

        super.init(type, pm);

        this.dragEnemiesToCenter();


        

    }

    protected onHitEnemy(self, other: Collider2D): void {
        this._contactPosition = other.node.worldPosition.clone();
        // 先调用父类方法处理伤害
        super.onHitEnemy(self, other);

        // 每隔1秒再造成两次伤害

        switch (this.runeId) {
            case 1:

                break;
    
            case 2:
                // 每隔1秒造成1次伤害
                this.schedule(() => {
                    this.em.hurt(this.damage); // 假设takeDamage是敌人组件中的方法
                    console.log("GaibangSkill3 onHitEnemy this.damage:", this.damage);
                }, 0.1, 10, 0);

                break;
            case 3:

                break;
            default:
                break;
        }


    }


    //把敌人拉到中心
    private dragEnemiesToCenter() {
        // 获取并存储DragEnemy节点
        this._dragEnemyNode = ObjectPoolManager.Instance.get(ESkill.DragEnemy);
        this._dragEnemyNode.setPosition(this.node.position);

       // node.destroy()
        
        

    }

    // protected onDestroy(): void {
    //     super.onDestroy();
    //     this._dragEnemyNode.destroy

        
    // }

    protected onDisable(): void {
        super.onDisable();


        // 如果有DragEnemy节点，回收它
        if (this._dragEnemyNode) {
            ObjectPoolManager.Instance.ret(this._dragEnemyNode);
            this._dragEnemyNode = null; // 清空引用
        }
    }






}